Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows access to payload in callback validator #16909

Merged
merged 1 commit into from Jan 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -34,7 +34,7 @@ public function validate($object, Constraint $constraint)

$method = $constraint->callback;
if ($method instanceof \Closure) {
$method($object, $this->context);
$method($object, $this->context, $constraint->payload);
} elseif (is_array($method)) {
if (!is_callable($method)) {
if (isset($method[0]) && is_object($method[0])) {
@@ -43,7 +43,7 @@ public function validate($object, Constraint $constraint)
throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
}

call_user_func($method, $object, $this->context);
call_user_func($method, $object, $this->context, $constraint->payload);
} elseif (null !== $object) {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
@@ -52,9 +52,9 @@ public function validate($object, Constraint $constraint)
$reflMethod = new \ReflectionMethod($object, $method);

if ($reflMethod->isStatic()) {
$reflMethod->invoke(null, $object, $this->context);
$reflMethod->invoke(null, $object, $this->context, $constraint->payload);
} else {
$reflMethod->invoke($object, $this->context);
$reflMethod->invoke($object, $this->context, $constraint->payload);
}
}
}
@@ -15,7 +15,6 @@
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\CallbackValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Validation;

class CallbackValidatorTest_Class
{
@@ -227,4 +226,28 @@ public function testAnnotationInvocationMultiValued()

$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
}

public function testPayloadIsPassedToCallback()
{
$object = new \stdClass();
$payloadCopy = null;

$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
'payload' => 'Hello world!',
]);
$this->validator->validate($object, $constraint);
$this->assertEquals('Hello world!', $payloadCopy);

$payloadCopy = null;
$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
]);
$this->validator->validate($object, $constraint);
$this->assertNull($payloadCopy);
}
}